package com.dime.basic.controller;

import java.util.List;
import java.io.IOException;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.dime.basic.api.domain.DimeBasehealthadvice;
import com.dime.basic.api.model.healthDto;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.dime.basic.service.IDimeBasehealthadviceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 健康建议Controller
 *
 * @author ruoyi
 * @date 2023-09-28
 */
@RestController
@RequestMapping("/basehealthadvice")
public class DimeBasehealthadviceController extends BaseController {
    @Autowired
    private IDimeBasehealthadviceService dimeBasehealthadviceService;

    /**
     * 查询健康建议列表
     */
    @RequiresPermissions("basic:basehealthadvice:list")
    @GetMapping("/list")
    public TableDataInfo list(DimeBasehealthadvice dimeBasehealthadvice) {
        startPage();
        List<DimeBasehealthadvice> list = dimeBasehealthadviceService.selectDimeBasehealthadviceList(dimeBasehealthadvice);
        return getDataTable(list);
    }

    /**
     * 导出健康建议列表
     */
    @RequiresPermissions("basic:basehealthadvice:export")
    @Log(title = "健康建议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DimeBasehealthadvice dimeBasehealthadvice) {
        List<DimeBasehealthadvice> list = dimeBasehealthadviceService.selectDimeBasehealthadviceList(dimeBasehealthadvice);
        ExcelUtil<DimeBasehealthadvice> util = new ExcelUtil<DimeBasehealthadvice>(DimeBasehealthadvice.class);
        util.exportExcel(response, list, "健康建议数据");
    }

    /**
     * 获取健康建议详细信息
     */
    @RequiresPermissions("basic:basehealthadvice:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(dimeBasehealthadviceService.selectDimeBasehealthadviceById(id));
    }

    /**
     * 新增健康建议
     */
    @RequiresPermissions("basic:basehealthadvice:add")
    @Log(title = "健康建议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DimeBasehealthadvice dimeBasehealthadvice) {
        return toAjax(dimeBasehealthadviceService.insertDimeBasehealthadvice(dimeBasehealthadvice));
    }

    /**
     * 修改健康建议
     */
    @RequiresPermissions("basic:basehealthadvice:edit")
    @Log(title = "健康建议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DimeBasehealthadvice dimeBasehealthadvice) {
        return toAjax(dimeBasehealthadviceService.updateDimeBasehealthadvice(dimeBasehealthadvice));
    }

    /**
     * 删除健康建议
     */
    @RequiresPermissions("basic:basehealthadvice:remove")
    @Log(title = "健康建议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(dimeBasehealthadviceService.deleteDimeBasehealthadviceByIds(ids));
    }


    @InnerAuth
    @PostMapping("/info/healthadviceList")
    public R<List<DimeBasehealthadvice>> getHealthadviceList(DimeBasehealthadvice dimeBasehealthadvice) {
        List<DimeBasehealthadvice> dimeBasehealthadviceList = dimeBasehealthadviceService.selectDimeBasehealthadviceList(dimeBasehealthadvice);
        return R.ok(dimeBasehealthadviceList);
    }


    @InnerAuth
    @GetMapping("/info/healthadviceBygroupIdsList/{groupids}")
    public R<List<DimeBasehealthadvice>> getHealthadviceBygroupIdsList(@PathVariable String[] groupids) {
        List<DimeBasehealthadvice> dimeBasehealthadviceList = dimeBasehealthadviceService.selectDimeBasehealthadviceBygroupIds(groupids);

        return R.ok(dimeBasehealthadviceList);
    }


    @InnerAuth
    @GetMapping("/info/healthDtoList/{groupids}")
    public R<List<healthDto>> healthDtoList(@PathVariable String[] groupids) {
        List<DimeBasehealthadvice> dimeBasehealthadviceList = dimeBasehealthadviceService.selectDimeBasehealthadviceBygroupIds(groupids);

        List<healthDto> healthDtoList = dimeBasehealthadviceList.stream().map(x -> {
            healthDto healthDto = new healthDto();
            healthDto.setGroupid(x.getGroupid());
            healthDto.setGroupname(x.getGroupname());
            return healthDto;
        }).distinct().collect(Collectors.toList());

        for (healthDto _health : healthDtoList) {
            List<DimeBasehealthadvice> temp = dimeBasehealthadviceList.stream().filter(x -> Objects.equals(x.getGroupid(), _health.getGroupid())).collect(Collectors.toList());
            _health.setDimeBasehealthadviceList(temp);
        }


        return R.ok(healthDtoList);
    }


}
